package com.ocom.aiotweb.service.impl;

import com.alibaba.fastjson.JSON;
import com.ocom.aiotweb.feignclient.AiotClientService;
import com.ocom.aiotweb.service.AccessUpgradePageService;
import com.ocom.aiotweb.vo.accessupgradepage.AccessUpgradePageOnShowVo;
import com.ocom.aiotweb.vo.accessupgradepage.ImportUpgradeVo;
import com.ocom.common.constant.MapKey;
import com.ocom.common.entity.aiot.EntityMjAppVerInfo;
import com.ocom.common.resp.Result;
import com.ocom.aiotweb.vo.accessupgradepage.DeleteVo;
import com.ocom.aiotweb.vo.accessupgradepage.OnShowVo;
import com.ocom.common.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AccessUpgradePageServiceImpl implements AccessUpgradePageService {

    @Autowired
    private AiotClientService aiotClientService;
    /**
     * 升级文件列表（UpgradePage/onShow）
     * */
    @Override
    public Result AccessUpgradePageOnShow(OnShowVo onShowVo) {
        Map<String, Object> map = new HashMap<>();
        MapKey mapKey = new MapKey();
        map.put(mapKey.getKey_comId(), onShowVo.getComId());
        map.put(mapKey.getKey_apkType(), onShowVo.getFileType());
        map.put(mapKey.getKey_page(), onShowVo.getPage());
        map.put(mapKey.getKey_size(), onShowVo.getSize());
        Result result = aiotClientService.AccessDeviceGetDeviceVerInfo(map);
        if (result.getCode()!= 200){
            return ResultUtil.error(result.getCode());
        }
        AccessUpgradePageOnShowVo accessUpgradePageOnShowVos = JSON.parseObject(JSON.toJSONString(result.getData()), AccessUpgradePageOnShowVo.class);
        ArrayList<Map> list1 = new ArrayList<>();
        if (accessUpgradePageOnShowVos != null){
            List<EntityMjAppVerInfo> list = accessUpgradePageOnShowVos.getList();
            for (EntityMjAppVerInfo entityMjAppVerInfo: list) {
                HashMap<Object, Object> map1 = new HashMap<>();
                map1.put("fileId",entityMjAppVerInfo.getId());
                map1.put(mapKey.getKey_fileType(),entityMjAppVerInfo.getApkType());
                map1.put(mapKey.getKey_newAppVer(),entityMjAppVerInfo.getAppVer());
                map1.put(mapKey.getKey_oldAppVer(),entityMjAppVerInfo.getOldAppVer());
                list1.add(map1);
            }

        }
        HashMap<Object, Object> map1 = new HashMap<>();
        map1.put("total",accessUpgradePageOnShowVos.getTotal());
        map1.put("list",list1);

        return ResultUtil.success(map1);
    }

    /**
     * 文件上传（UpgradePage/importUpgrade）
     * */
    @Override
    public Result AccessUpgradePageImportUpgrade(ImportUpgradeVo importUpgradeVo){
        Map<String, Object> map = new HashMap<>();
        MapKey mapKey = new MapKey();
        map.put(mapKey.getKey_apkType(), importUpgradeVo.getFileType());
        map.put(mapKey.getKey_appVer(), importUpgradeVo.getNewAppVer());
        map.put(mapKey.getKey_oldAppVer(), importUpgradeVo.getOldAppVer());
        map.put(mapKey.getKey_apkFileUrl(), importUpgradeVo.getFileUrl());
        map.put(mapKey.getKey_status(),0);
        return aiotClientService.AccessDeviceAddDeviceVerInfo(map);
    }

    /**
     * 升级文件删除（UpgradePage/delete）
     * */
    @Override
    public Result AccessUpgradePageDelete(DeleteVo deleteVo) {
        Long id = deleteVo.getFileId();
        return aiotClientService.AccessDeviceDeleteDeviceVerInfo(id);
    }
}
